#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char** read_input(int* n);
void print_vowel_consonant_count (char** names_array, int n_names);

int main (void)
{
    /*
       We are going to have to store many names.
       Each name is an array of char. So, each name is really a character pointer.
       Now, we are going to have to store many such names, i.e. many such pointers.
       We are going to need an array of such pointers then. I mean, an array of *char.
       What is an array? Merely a pointer to the first element.

       names[0] : Will have a pointer to the 0th char of the 0th name
       names[1] : Will have a pointer to the 0th char of the 1st name
       names[2] : Will have a pointer to the 0th char of the 2nd name
       names[3] : Will have a pointer to the 0th char of the 3rd name
       etc...

       So,
       names[0] is a *char
       names[1] is a *char
       names[2] is a *char

       Therefore, names is a pointer to *char, or names is **char.

    */
    char** names   = NULL;   // Ragged array of strings
    int    n_names = 0;      // How many strings have we stored?

    names = read_input(&n_names);
    print_vowel_consonant_count(names, n_names);
    return 0;
}

void print_vowel_consonant_count (char** names_array, int n_names)
{
    int i;
    int total_vowels     = 0;
    int total_consonants = 0;
    char** names = names_array;

    printf("Vowels    Consonants       String\n");
    printf("-----------------------------------------------------------------\n");
    for (i = 0; i<n_names; i++)
        {
        int len = strlen(names[i]);
        int vowels = 0;
        int consonants = 0;
        int j;
        for (j = 0; j<len; j++)
            {
            char c = names[i][j];
            if (
                    (c == 'a')
                 || (c == 'e')
                 || (c == 'i')
                 || (c == 'o')
                 || (c == 'u')
                 || (c == 'y')
                 || (c == 'A')
                 || (c == 'E')
                 || (c == 'I')
                 || (c == 'O')
                 || (c == 'U')
                 || (c == 'Y')
               )
                {
                vowels++;
                total_vowels++;
                }
            else if (
                         (('a' <= c) && (c <= 'z'))
                      || (('A' <= c) && (c <= 'Z'))
                    )
                {
                consonants++;
                total_consonants++;
                }
            }
        printf("%02d        %02d               %s", vowels, consonants, *(names+i));
        }
    printf("-----------------------------------------------------------------\n");
    printf("%02d        %02d               Total\n", total_vowels, total_consonants);
}


char** read_input (int* n)
{
    char **names = NULL;
    char buffer[256];
    FILE *F;

    F = fopen("01-lab3new-input.txt", "r");
    if (F == NULL)
        {
        printf("Error: Could not open 01-lab3new-input.txt\n");
        exit(1);
        }

    *n = 0;
    while (fgets(buffer, 255, F))  // Read a line from the file
        {
        names = (char**)realloc(names, sizeof(char*) * (*n + 1));
        names[*n] = (char*)calloc(strlen(buffer)+1, 1);    // Allocate a byte more than the the input string
                                                           // because each char is 1 byte long
        strncpy(names[*n], buffer, strlen(buffer) + 1);    // then copy the input string to that location
        *n += 1;
        }
    fclose(F);
    return names;
}
